package prvydevj;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

/**
 * Created with IntelliJ IDEA.
 * User: TOM
 * Date: 8.6.13
 * Time: 16:27
 * To change this template use File | Settings | File Templates.
 */
public class InvoiceForm implements Printable {
    private JTable items;
    private JScrollPane panel;
    private JPanel mainPanel;
    private JLabel suma;
    private JButton printButton;
    private JLabel org1;
    private JLabel org2;
    private JLabel org3;
    private JLabel org4;
    private JLabel date1;
    private JLabel date2;
    private JLabel org5;
    private JLabel osb1;
    private JLabel osb2;
    private JLabel osb3;
    private JLabel osb4;
    private JLabel osb5;
    private JLabel title;
    DecimalFormat df;
    DateFormat datef = new SimpleDateFormat("dd. MM. yyyy");

    public InvoiceForm(InvoiceManager manager, Transaction transaction)
    {
        title.setText("FAKTURA - DOKLAD Č. " + transaction.getId());
        df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);

        InvoiceItemTableModel invoiceModel = new InvoiceItemTableModel();
        items.setModel(invoiceModel);

        suma.setText("K uhradě celkem: " + df.format(transaction.getCastka()));

        for ( InvoiceItem item : transaction.getItems())
            invoiceModel.add(item);

        if (transaction.getPrijem())
        {
            osb1.setText(manager.getOwnerName());
            osb2.setText(manager.getOwnerCity() + ", " + manager.getOwnerAddress() + " " + manager.getOwnerPsc() );
            osb3.setText(manager.getOwnerIco());
            osb4.setText(manager.getOwnerDic());
            osb5.setText("číslo účtu: " + manager.getOwnerAccount());

            org1.setText(transaction.getNazev());
            org2.setText(transaction.getMesto() + ", " + transaction.getUlice() + " " + transaction.getPsc() );
            org3.setText(transaction.getIco());
            org4.setText(transaction.getDic());
            org5.setText("číslo účtu: " + transaction.getUcet());
        }
        else
        {
            org1.setText(manager.getOwnerName());
            org2.setText(manager.getOwnerCity() + ", " + manager.getOwnerAddress() + " " + manager.getOwnerPsc() );
            org3.setText(manager.getOwnerIco());
            org4.setText(manager.getOwnerDic());
            org5.setText("číslo účtu: " + manager.getOwnerAccount());

            osb1.setText(transaction.getNazev());
            osb2.setText(transaction.getMesto() + ", " + transaction.getUlice() + " " + transaction.getPsc() );
            osb3.setText(transaction.getIco());
            osb4.setText(transaction.getDic());
            osb5.setText("číslo účtu: " + transaction.getUcet());
        }

        date1.setText("Vystaveni: " + datef.format(transaction.getVystaveni()));
        date2.setText("Splatnost: " + datef.format(transaction.getSplatnost()));

    }

    public static void Show(InvoiceManager manager, Transaction transaction) {
        final InvoiceForm form = new InvoiceForm(manager, transaction);
        JFrame frame = new JFrame("Faktura cislo " + transaction.getId());

        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setContentPane(form.mainPanel);

        frame.setPreferredSize(new Dimension(600, 490));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        form.printButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPrintable(form);
                boolean ok = job.printDialog();
                if (ok) {
                    try {
                        job.print();
                    } catch (PrinterException ex) {
              /* The job did not successfully complete */
                    }
                }
            }
        });
        //JOptionPane.showMessageDialog(null, transaction.getCastka(), "", JOptionPane.INFORMATION_MESSAGE);
    }

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 0) { /* We have only one page, and 'page' is zero-based */
            return NO_SUCH_PAGE;
        }

        /* User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D)graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        /* Now print the window and its visible contents */
        mainPanel.printAll(graphics);

        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }
}
